package com.mifengs.mall.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mifengs.mall.R;
import com.mifengs.mall.entity.AdvertBean;
import com.mifengs.mall.util.ImageLoadUtil;
import com.mifengs.mall.util.TimeUitl;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by lihaoqi on 2017/10/31.
 */

public class TwoFourItemAdapter extends BaseQuickAdapter<AdvertBean,BaseViewHolder> {

    private Context mContext;
    int type = 0; //1 表示需要加秒杀时间
    private final Timer mTimer;
    private TimerTask mTimerTask;
    private MyHandler mHandler;
    public TwoFourItemAdapter(@LayoutRes int layoutResId, @Nullable List<AdvertBean> data, Context mContext,int type) {
        super(layoutResId, data);
        this.mContext = mContext;
        this.type = type;
        mTimer = new Timer();
    }

    @Override
    protected void convert(BaseViewHolder helper, AdvertBean item) {
        ImageView imageView = helper.getView(R.id.pro_img);
        ImageLoadUtil.displayImage(mContext,item.getImg(),imageView);
        LinearLayout linearLayout = helper.getView(R.id.linear_time);
        if(type== 1){
            if(helper.getPosition()==1){
                if(!"0".equals(item.getTimer())){
                    linearLayout.setVisibility(View.VISIBLE);
                    TextView timeText = helper.getView(R.id.time_text);
                    TextView countDownTime = helper.getView(R.id.count_down_time);
                    timeText.setText(item.getSpieltag());
//                countDownTime.setText(TimeUitl.calculateText(item.getTimer()));
                    mHandler = new MyHandler(item.getTimer(),countDownTime);
                    if(mTimerTask==null){
                        mTimerTask = new TimerTask() {
                            @Override
                            public void run() {
                                mHandler.sendEmptyMessage(1);
                            }
                        };
                        mTimer.schedule(mTimerTask,1000,1000);
                    }

                }else{
                    linearLayout.setVisibility(View.INVISIBLE);
                }
            }else{
                linearLayout.setVisibility(View.INVISIBLE);
            }

        }
    }

    class MyHandler extends Handler {

        private String time;
        private TextView textView;

        public MyHandler(String time, TextView textView) {
            this.time = time;
            this.textView = textView;
        }

        @Override
        public void handleMessage(Message msg) {
            switch(msg.what){
                case 1:
                    textView.setText(TimeUitl.calculateText(time));
                    break;
            }
        }
    }

}